/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing.workers;

import cz.muni.fi.pv168.uloha3.common.DBUtils;
import cz.muni.fi.pv168.uloha3.entities.Owner;
import cz.muni.fi.pv168.uloha3.exceptions.OwnerManagerException;
import cz.muni.fi.pv168.uloha3.managers.impl.OwnerManagerImpl;
import cz.muni.fi.pv168.uloha3.swing.OwnersTableModel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author stefan
 */
public class OwnerDeleteSwingWorker extends SwingWorker<Boolean, Void> {
    
    private final Owner owner;
    private final OwnersTableModel model;
    
    public OwnerDeleteSwingWorker(Owner owner, OwnersTableModel model) {
        this.owner = owner;
        this.model = model;
    }

    @Override
    protected Boolean doInBackground() throws Exception {
        DataSource dataSource = DBUtils.initConnector();
        
        OwnerManagerImpl managerOwner = new OwnerManagerImpl();
        managerOwner.setDataSource(dataSource);
        
        try {
            managerOwner.deleteOwner(owner);
        } catch(OwnerManagerException ex) {
            Logger.getLogger(OwnerDeleteSwingWorker.class.getName()).log(Level.WARNING, ex.getMessage());
            JOptionPane.showMessageDialog(null, "Owner could not be deleted (" + ex.getMessage() + ")");
            return false;
        }
        
        model.removeOwner(owner);
        JOptionPane.showMessageDialog(null, "Owner deleted");
        return true;
    }
    
}
